package com.token.sysflow.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.token.base.JsonGenerator;
import com.token.sql.IntegrateSql;
import com.token.sql.PrepareSql;
import com.token.sysflow.bean.FSysFlowApproveLog;
import com.token.sysflow.enumerate.ActionTypeEnum;
import com.token.sysflow.enumerate.ApprovalTypeEnum;
import com.token.sysflow.hander.ApproveProcess;
import lombok.Getter;
import lombok.Setter;

import java.sql.ResultSet;

/**
 * @author chenjianxiong  2020/6/30
 */
public class GetApproveProcessImpl extends AbstractFlowHandlerImpl {
    @Setter
    @Getter
    private int approveLog = 0;
    @Override
    public void initParameter(IntegrateSql iSql) throws Exception {
        PrepareSql psql = new PrepareSql(iSql);
        psql.setPrepare("select * from v_Sys_ApproveProcess_ing where KeyUnid=?");
        psql.setPstValue(1,this.keyUnid);
        ResultSet rs = psql.executeQuery();
        JSONArray array = new JSONArray();
        if(rs.next()){//审批中
            JSONObject json = JsonGenerator.toJson(rs);
            json.put("SysAddTime",rs.getString("SysAddTime"));
            json.put("SysEditTime",rs.getString("SysEditTime"));
            json.remove("SysId");
            array.add(json);

            //子流程
            if(ApprovalTypeEnum.MZLC.getId() == json.getIntValue("ApprovalType")){
                rs.close();
                psql.setPrepare("select p.*,s.StepName from F_Sys_ApproveProcess p inner join Flow_StepBasic s on p.StepId=s.StepId where ParentId=?");
                psql.setPstValue(1,this.keyUnid);
                rs = psql.executeQuery();
                JSONArray array1 = new JSONArray();
                while(rs.next()){
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("SubKeyUnid",rs.getString("KeyUnid"));
                    jsonObject.put("Subject",rs.getString("Subject"));
                    jsonObject.put("ApprovalType",rs.getString("ApprovalType"));
                    jsonObject.put("FlowId",rs.getString("FlowId"));
                    jsonObject.put("StepId",rs.getString("StepId"));
                    jsonObject.put("StepName",rs.getString("StepName"));
                    doSetSubFlowAprovingVth(iSql,rs.getInt("SysId"),jsonObject);
                    array1.add(jsonObject);
                }

                json.put("SubFlow",array1);

            }

        }else{//审批完成
            rs.close();
            psql.setPrepare("select * from v_Sys_ApproveProcessed where KeyUnid=?");
            psql.setPstValue(1,this.keyUnid);
            rs = psql.executeQuery();
            if(rs.next()) {
                JSONObject json = JsonGenerator.toJson(rs);
                json.put("SysAddTime",rs.getString("SysAddTime"));
                json.put("SysEditTime",rs.getString("SysEditTime"));
                json.remove("SysId");
                array.add(json);
            }
        }

        this.setReturnJson("data",array);
        rs.close();

        //ParentId


        //办理意见
        if(approveLog == 1) {
            StringBuffer sql = new StringBuffer(100);
            sql.append("select FormFlowName as flowName,FormStepName as StepName,ActionTypeTitle,ApproverName,ApprovDesc,StartTime,EndTime,Options from ")
                    .append(FSysFlowApproveLog.TABLE_NAME)
                    .append(" where KeyUnid=?");
            psql.setPrepare(sql);
            psql.setPstValue(1, this.keyUnid);
            rs = psql.executeQuery();
            this.setReturnJson("approveLog", JsonGenerator.toJsonArray(rs));
            rs.close();
        }
    }

    /**
     * 读取子流程的办理人
     * @param iSql
     * @param sysId
     * @param jsonObject
     * @throws Exception
     */
    private void doSetSubFlowAprovingVth(IntegrateSql iSql,int sysId, JSONObject jsonObject) throws Exception{
        PrepareSql prepareSql = new PrepareSql(iSql);
        prepareSql.setPrepare("select PersonNumber,PersonName from F_Sys_ApprovingVth vth inner join F_Sys_Personnel p on vth.Approver=p.PersonNumber and p.SysId=? where vth.keyunid=? and vth.status=?");
        prepareSql.setPstValue(1,sysId);
        prepareSql.setPstValue(2,jsonObject.getString("SubKeyUnid"));
        prepareSql.setPstValue(3,ApproveProcess.STATUS_IND);

//        System.out.println("jsonObject = " + jsonObject);
//        System.out.println("sysId = " + sysId);
//        System.out.println("jsonObject.getString(\"keyUnid\") = " + jsonObject.getString("SubKeyUnid"));
//        System.out.println("ApproveProcess.STATUS_IND = " + ApproveProcess.STATUS_IND);

        ResultSet rs = prepareSql.executeQuery();
        StringBuilder number = new StringBuilder(50);
        StringBuilder name = new StringBuilder(50);
        boolean isHas = false;
        if(rs.next()){
            do{
                if(isHas){
                    number.append(",");
                    name.append(",");
                }
                number.append(rs.getString("PersonNumber"));
                name.append(rs.getString("PersonName"));
                isHas = true;
            }while(rs.next());
        }
        rs.close();
        prepareSql.close();
        jsonObject.put("Approver",number);
        jsonObject.put("ApproverName",name);

    }

    @Override
    public ActionTypeEnum getActionType() {
        return ActionTypeEnum.APPROVEPROCESS;
    }

    @Override
    public void handleReturnResult() throws Exception {

    }
}
